package my.jwds.p121;

public class MaxProfit {
    public static void main(String[] args) {
        MaxProfit o = new MaxProfit();
        System.out.println(o.maxProfit(new int[]{7,1,5,3,6,4}));
        System.out.println(o.maxProfit(new int[]{7,6,4,3,1}));
    }

    public int maxProfit(int[] prices) {
        int max = 0,dayProfit = prices[0];
        for (int i = 1; i < prices.length; i++) {
            int temp = prices[i] - dayProfit;
            if (temp < 0)dayProfit = prices[i];
            if (temp > max)max = temp;
        }
        return max;
    }
}
